home *** CD-ROM | disk | FTP | other *** search
- Path: bigboote.WPI.EDU!nntp!keithbar
- From: keithbar@owl.WPI.EDU (Keith Barrett)
- Newsgroups: comp.lang.c
- Subject: Question on string literals and char arrays
- Date: 23 Feb 1996 00:22:11 GMT
- Organization: Worcester Polytechnic Institute, Worcester, MA 01609-2280
- Message-ID: <KEITHBAR.96Feb22192211@owl.WPI.EDU>
- NNTP-Posting-Host: owl.wpi.edu
-
-
- This may have been asked before but here it is:
-
- Could someone explain to me in very great detail what is the
- difference between:
-
- main()
- {
- char *s;
- s = "Keith";
- printf("%s\n", s);
- }
-
- and
-
- main()
- {
- char s[6];
- s = "Keith";
- printf("%s\n", s);
- }
-
- In the compiler I use (gcc version 2.7.2) the second one will not
- compile and the first does and works. I understand the surface of this
- problem and I am a grad student in computer science so I am looking
- for details.
-
- I realize the s in the second example acts as a a const char * and
- theirfore can't be changed. That is why
-
- char s[6] = "Keith";
-
- is allowed.
-
- My questions are:
-
- 1) where exactly is the string literal located. I realize it has
- global scope so I assume it is created right in the data segment.
-
- 2) what does it evaluate to. I think is is a static const char * const
- (a static constant char pointer to a constant char).
-
- "Keith"[2] evaluates to a const char right
-
- 3) I know that a pointer variable of type (char *) is created for the
- first example but what about example two. Is s a (const char *)? Some
- books say that s in this case is not even created. Because it is a
- const char pointer and can't change it is never created. The real
- address is just used everywhere s is rather than looking it up.
- Assuming a non optimizing compiler.
-
-
- Any help would be appreciated,
- Keith Barrett
-